package com.jmz.apt.api.dao;

import com.jmz.apt.api.Provider.AdminUserProvider;
import com.jmz.apt.api.entity.dbEntity.JmzAdminRole;
import com.jmz.apt.api.entity.dbEntity.JmzAdminUser;
import com.jmz.apt.api.message.request.Condition;
import com.jmz.apt.api.message.request.Query;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.dao.DataAccessException;

import java.util.List;

/**
 * Created by fanglexue on 2019/10/23.
 */

@Mapper
public interface AdminDao {

    @SelectProvider(type = AdminUserProvider.class, method="select")
    public List<JmzAdminUser> findAdmin(String columns, List<Condition> condition, Query query);

    @SelectProvider(type = AdminUserProvider.class, method="getCount")
    public Integer count(List<Condition> condition);

    @SelectProvider(type = AdminUserProvider.class, method="insert")
    public Integer saveAdmin(JmzAdminUser jmzAdminUser) throws DataAccessException;

    @UpdateProvider(type = AdminUserProvider.class, method="update")
    public Boolean updateAdmin(JmzAdminUser jmzAdminUser) throws DataAccessException;

    @SelectProvider(type = AdminUserProvider.class, method="selectOne")
    public JmzAdminUser findAdminOne(JmzAdminUser jmzAdminUser);

    /**
     * Find administrator information with the given admin id;
     * @param adminId
     * @return
     */
    @Select("SELECT * FROM jmz_admin_user WHERE id = #{adminId}")
    @Results(
            {
                    @Result(id = true, column = "id", property = "id"),
                    @Result(column = "name", property = "name"),
                    @Result(column = "id", property = "adminRoles", many = @Many(select = "com.jmz.apt.api.dao.AdminDao.findAdminRoles", fetchType = FetchType.LAZY)),
            }
    )
    public JmzAdminUser findAdminJoinRoles(Integer adminId);

    @Select("SELECT ar.* FROM jmz_admin_role as ar left join jmz_admin_user_roles as aur on ar.id=aur.role_id WHERE aur.admin_id = #{adminId}")
    public JmzAdminRole findAdminRoles(Integer adminId);

    @SelectProvider(type = AdminUserProvider.class, method="selectRepeatOne")
    public JmzAdminUser findRepeatOne(JmzAdminUser jmzAdminUser);

    @SelectProvider(type = AdminUserProvider.class, method="delete")
    public JmzAdminUser delete(JmzAdminUser jmzAdminUser);

    @Update("update jmz_admin_user t set t.password = #{password},t.update_time = #{updateTime} where t.id = #{id}")
    int changePassword(JmzAdminUser user);


}
